﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    /// <summary>
    /// 切换主服务操作
    /// </summary>
    public class ReqSwitchMainServerHandler : BaseHandler<ReqSwitchMainServerData, 
        RespSwitchMainServerData>
    {
        public ReqSwitchMainServerHandler()
            : base(SCENARIO.CONNECT, COMMAND.CONNECT.SWITCHMAINSERVER)
        {
        }

        protected override RespSwitchMainServerData OnHandle(Data.MainServiceData.Request request,
            ReqSwitchMainServerData requestData)
        {
            Logger.DebugToTag("MainService", string.Format("终端请求切换主服务为{0}:{1},域id:{2}",
                requestData.MainServerIP,
                requestData.MainServerPort,
                request.Header.DomainId));

            RespSwitchMainServerData responseData = new RespSwitchMainServerData();
            responseData.ServerIP = CZEnvironment.Instance.ServerIP;
            responseData.Port = CZEnvironment.Instance.ServerPort;
            responseData.MainServerIP = requestData.MainServerIP;
            responseData.MainServerPort = requestData.MainServerPort;

            if (!string.IsNullOrEmpty(request.Header.DomainId))
            {
                CacheData.ControlDomain domain = CacheData.ServerData.Instance.GetDomain(request.Header.DomainId);
                if (domain != null)
                {
                    CacheData.Client client = domain.GetClient(request.Header.UserIdentity);
                    if (client != null)
                    {
                        if (string.Equals(client.UserIdentity, domain.OperatorIdentity))
                        {
                            domain.UpdateMainServer(requestData.MainServerIP, requestData.MainServerPort);

                            Logger.WarningToTag("MainService", string.Format("将域id:{0}的主服务器设置为{1}:{2}",
                                    request.Header.DomainId,
                                    requestData.MainServerIP,
                                    requestData.MainServerPort));
                            responseData.IsSuccess = true;
                        }
                        else
                        {
                            responseData.IsSuccess = false;
                            responseData.Message = "用户不是域的操作员";
                            Logger.WarningToTag("MainService", string.Format("请求切换主服务器为{0}:{1},但是域{2}的操作员是{3}，不是{4}", requestData.MainServerIP, requestData.MainServerPort, request.Header.DomainId,
                                domain.OperatorIdentity,
                                request.Header.UserIdentity));
                        }
                    }
                    else
                    {
                        responseData.IsSuccess = false;
                        responseData.Message = "用户不存在";
                        Logger.WarningToTag("MainService", string.Format("请求切换主服务器为{0}:{1},但是域{2}里没有用户{3}", requestData.MainServerIP, requestData.MainServerPort, request.Header.DomainId,
                            request.Header.UserIdentity));
                    }
                }
                else
                {
                    responseData.IsSuccess = false;
                    responseData.Message = "域不存在";
                    Logger.WarningToTag("MainService", string.Format("请求切换主服务器为{0}:{1},但是域{2}不存在", requestData.MainServerIP, requestData.MainServerPort, request.Header.DomainId));
                }
            }
            else
            {
                responseData.IsSuccess = false;
                responseData.Message = "请求中没有域id";
                Logger.WarningToTag("MainService", string.Format("请求切换主服务器为{0}:{1},但是请求中没有域id", requestData.MainServerIP, requestData.MainServerPort));
            }
            //只有主服务才返回，否则收到备用服务器的应答，client不会更新
            if (string.Equals(responseData.ServerIP, responseData.MainServerIP) && responseData.Port == responseData.MainServerPort)
            {
                return responseData;
            }
            return null;
        }
    }
}
